<?php

namespace app\api\controller;

use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\user\User;

use app\common\controller\Api;

/**
 * 首页接口
 * @ApiInternal
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        // $url = "https://open-barcode.xpyun.net/api/openapi/sprinter/printImage";   //
        // $param = array(
        //     "user" => "baihuasenlin2023@163.com",
        //     "timestamp" => (string)time(),
        //     "sn" => "000045T4D8F8C4A",
        //     "content" => base64EncodeImage(ROOT_PATH . 'public' . '/uploads/20231008/2e6563fecafe3babe65457fabc41f982.jpg'),
        // );
        // $param['sign'] = sign($param);
        // $result = json_decode(httpRequest($url, "POST", json_encode($param), ['Content-Type: application/json; charset=UTF-8']), true);

        // $url = "https://open-barcode.xpyun.net/api/openapi/sprinter/printLabel";   //
        // $param = array(
        //     "user" => "baihuasenlin2023@163.com",
        //     "timestamp" => (string)time(),
        //     "sn" => "000045T4D8F8C4A",
        //     "content" => '<PAGE w="100.0" l="10.0">
        //     <LABEL id="1">
        //     <TEXT x="1" y="1" xw="1" yh="1">店铺：某某店</TEXT>
        //     <TEXT x="1" y="2" xw="1" yh="1">顾客：某某</TEXT>
        //     <TEXT x="1" y="3" xw="1" yh="1">顾客电话：16888888888</TEXT>
        //     <TEXT x="1" y="4" xw="1" yh="1">单号：wx16888888888</TEXT>
        //     <TEXT x="1" y="5" xw="1" yh="1">接单人：某某</TEXT>
        //     <BARC x="1" y="6" h="50" s="2">1234567890</BARC>
        //     <TEXT x="1" y="10" xw="1" yh="1">1234567890</TEXT>
        //     </LABEL>
        //     </PAGE>',
        // );
        // $param['sign'] = sign($param);
        // $result = json_decode(httpRequest($url, "POST", json_encode($param), ['Content-Type: application/json; charset=UTF-8']), true);
        // // $result['param'] = $param;
        // return json($result);
        // for ($i = 0; $i < -1; $i++) {
        //     var_dump(88);
        // }

        // $order = Order::with('item')->where('id', 100)->find();
        // $user = User::get($order['user_id']);
        // // 触发订单支付完成事件
        // $data = ['order' => $order, 'user' => $user];
        // // \think\Hook::listen('order_paid_after', $data);
        // \think\Hook::listen('order_payed_after', $data);

        $user = \addons\shopro\model\UserOauth::where('user_id', 43)->find();
        $user = \app\admin\model\shopro\wechat\Fans::where('unionid', $user['unionid'] ?? "-1")->find();
        $wxMiniProgram = \addons\shopro\model\Config::getEasyWechatConfig('wxMiniProgram');
        $wxOfficialAccount = \addons\shopro\model\Config::getEasyWechatConfig('wxOfficialAccount');
        $config = [
            'app_id'        => $wxOfficialAccount['app_id'],
            'secret'        => $wxOfficialAccount['secret'],
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ];
        $app = \EasyWeChat\Factory::officialAccount($config);
        //  通过公众号发送
        $res = $app->template_message->send([
            'touser' => $user['openid'],
            'template_id' => 'hbaWQ4Zh0JsdDr_sX9Q19_qDuf8Hz20sdXSg8LAPypY', //取衣提醒
            'url' => '',
            'miniprogram' => [
                'appid' => $wxMiniProgram['app_id'],
                'pagepath' => 'pages/index/index',
            ],
            'data' => [
                'character_string2' => ["value" => 'abv123'],
                'character_string13' => '1',
                'time8' => date('Y-m-d H:i:s', time()),
                'thing3' => '取衣地点',
            ],
        ]);
        $res = $app->template_message->send([
            'touser' => $user['openid'],
            'template_id' => 'HUbaiNdi4xV6u2pZqq5zHukoo9CBEKCnXgZkQoly8tY', //充值成功通知
            'url' => '',
            'miniprogram' => [
                'appid' => $wxMiniProgram['app_id'],
                'pagepath' => 'pages/index/index',
            ],
            'data' => [
                'thing7' => ["value" => '商户名称'],
                'amount3' => '充值金额',
                'phrase11' => '充值方式',
                'time2' => date('Y-m-d H:i:s', time()),
                // 'thing3' => '取衣地点',
            ],
        ]);
        $res = $app->template_message->send([
            'touser' => $user['openid'],
            'template_id' => 'hl4j01EkKXXw_2ylrrjfhaQ38LxvcalVwT_bLj9OBTI', //下单成功通知
            'url' => '',
            'miniprogram' => [
                'appid' => $wxMiniProgram['app_id'],
                'pagepath' => 'pages/index/index',
            ],
            'data' => [
                'character_string13' => ["value" => 'abv123'],
                'thing2' => '商品名称',
                'thing12' => '购买用户',
                'amount4' => '1',
                'time5' => date('Y-m-d H:i:s', time()),
                // 'thing3' => '取衣地点',
            ],
        ]);
        $res = $app->template_message->send([
            'touser' => $user['openid'],
            'template_id' => 'wfHE-NpfOt_5MWkSCq3ly68B_cqH-6rO-3V8icFLKzE', //订单取消通知
            'url' => '',
            'miniprogram' => [
                'appid' => $wxMiniProgram['app_id'],
                'pagepath' => 'pages/index/index',
            ],
            'data' => [
                'character_string7' => ["value" => 'abv123'],
                'thing2' => '商品名称',
                'amount11' => '订单金额',
                'amount4' => '退款金额',
                'time5' => date('Y-m-d H:i:s', time()),
                // 'thing3' => '取衣地点',
            ],
        ]);
        var_dump($user['openid']);
        var_dump($res);
        $this->success('请求成功');

        // 洗衣单号
        // {{character_string2.DATA}}
        // 衣物数量
        // {{character_string13.DATA}}
        // 清洗完成时间
        // {{time8.DATA}}
        // 取衣地点
        // {{thing3.DATA}}
        $param = json_encode($new_params);
        //POST参数
        $res = json_decode(\app\admin\controller\shopro\user\User::httpRequest($qcode, $param, "POST"), true);
        var_dump($user['openid']);
        var_dump($res);
        $this->success('请求成功');
    }
}
